#include "MainForm.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Graphics;

MainForm::MainForm(void) {
}

MainForm::~MainForm(void) {
}

bool MainForm::Initialize() {
	// Construct an XML form
	Construct(L"IDF_MAINFORM");

	return true;
}

result MainForm::OnInitializing(void) {
	result r = E_SUCCESS;

	// softkey init
	SetSoftkeyActionId(SOFTKEY_0, PAGE_LEFT);
	SetSoftkeyText(SOFTKEY_0, "<");
	AddSoftkeyActionListener(SOFTKEY_0, *this);
	SetSoftkeyActionId(SOFTKEY_1, PAGE_RIGHT);
	SetSoftkeyText(SOFTKEY_1, ">");
	AddSoftkeyActionListener(SOFTKEY_1, *this);

	// optionkey init
	this->SetOptionkeyActionId(OPTION_KEY_ACTION);
	AddOptionkeyActionListener(*this);
	__pOptionsMenu = new OptionMenu();
	__pOptionsMenu->Construct();
	__pOptionsMenu->AddItem(L"Rendezés...", 204);
	__pOptionsMenu->AddItem(L"Új elem felvétele", NEW_MODAL_START);
	__pOptionsMenu->AddSubItem(0, L"csökkenő sorrend", SORT_ASC);
	__pOptionsMenu->AddSubItem(0, L"növekvő sorrend", SORT_DESC);
	__pOptionsMenu->AddActionEventListener(*this);

	// reading images
	Osp::Media::Image * bitmapDecoder;
	bitmapDecoder = new Osp::Media::Image();
	bitmapDecoder->Construct();

	bitmapDeleteActive = bitmapDecoder->DecodeN(L"/Res/delete_active.png", BITMAP_PIXEL_FORMAT_ARGB8888);
	bitmapDeletePassive = bitmapDecoder->DecodeN(L"/Res/delete_passive.png", BITMAP_PIXEL_FORMAT_ARGB8888);

	__pList = static_cast<CustomList *>(GetControl(L"CUSTOMLIST"));
	__pList->AddCustomItemEventListener(*this);

	// defining the custom itemformat
	__pCustomListItemFormat = new CustomListItemFormat();
	__pCustomListItemFormat->Construct();

	// adding elements to the custom format
	__pCustomListItemFormat->AddElement(TEXT_LISTENER, Rectangle(20,30,390,90),40, Color::COLOR_WHITE, Color::COLOR_GREY);
	__pCustomListItemFormat->AddElement(DELETE_LISTENER, Rectangle(395,10,80,80));

	// selecting which elements can receive touch events
	__pCustomListItemFormat->SetElementEventEnabled(DELETE_LISTENER, true);

	// modal init
	__pNewRowModal = new Popup();
	__pNewRowModal->Construct(L"NewRowModal");
	__pNameEditField = static_cast<EditField *>(__pNewRowModal->GetControl("NAME_EDITFIELD",false));
	__pPwdEditField = static_cast<EditField *>(__pNewRowModal->GetControl("PWD_EDITFIELD",false));
	__pAddButton = static_cast<Button *> (__pNewRowModal->GetControl("ADDBUTTON",false));
	if (__pAddButton != null)
	{
		__pAddButton->SetActionId(NEW_MODAL_ADD);
		__pAddButton->AddActionEventListener(*this);
	}
	__pCloseAddButton = static_cast<Button *> (__pNewRowModal->GetControl("CLOSEADDBUTTON",false));
	if (__pCloseAddButton != null)
	{
		__pCloseAddButton->SetActionId(NEW_MODAL_X);
		__pCloseAddButton->AddActionEventListener(*this);
	}

	// initial settings
	sort = true;
	currentPage = 0;
	rowsPerPage = 7;
	arraylist.Construct();

	// db res init
	databasehandler.Construct();
	// populating list w/ existing rows
	RefreshList(true);

	return r;
}

result MainForm::OnTerminating(void) {
	result r = E_SUCCESS;

	this->RemoveAllControls();
	__pNewRowModal->RemoveAllControls();
	__pNewRowModal->~Popup();
	delete bitmapDeletePassive;
	delete bitmapDeleteActive;
	databasehandler.~DatabaseHandler();
	delete enumerator;
	arraylist.~ArrayListT();

	return r;
}

void MainForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId) {
	switch (actionId) {
	case NEW_MODAL_START: {
		__pNewRowModal->SetShowState(true);
		__pNewRowModal->Show();
	}
		break;
	case OPTION_KEY_ACTION:
	{
		__pOptionsMenu->SetShowState(true);
		__pOptionsMenu->Show();
	}
		break;
	case NEW_MODAL_ADD: {
		AppLog("Add Button clicked! \n");
		String a, b;
		a = __pNameEditField->GetText();
		b = __pPwdEditField->GetText();
		databasehandler.Persist(a, b);
		__pNewRowModal->SetShowState(false);
		RefreshList(true);
	}
		break;
	case NEW_MODAL_X: {
		AppLog("Quit Button clicked! \n");
		__pNewRowModal->SetShowState(false);
		this->RequestRedraw();
	}
		break;
	case LIST_ID: {
		AppLog("list clicked! \n");
	}
		break;
	case SORT_ASC: {
		sort = true;
		RefreshList(sort);
	}
		break;
	case SORT_DESC: {
		sort = false;
		RefreshList(sort);
	}
		break;
	case PAGE_LEFT: {
		if (currentPage > 0)
			currentPage--;
		RefreshList(sort);
	}
		break;

	case PAGE_RIGHT: {
		currentPage++;
		RefreshList(sort);
	}
		break;
		//
	default:
		break;
	}
}

void MainForm::RefreshList(bool sorrend) {
	enumerator = databasehandler.getNItems(sorrend, currentPage, rowsPerPage);

	if (!enumerator) {
		AppLog("a lekerdezes ures eredmenyhalmazzal tert vissza");
		currentPage--;
		RefreshList(sorrend);
	}
	if (enumerator) {
		arraylist.RemoveAll();

		String egy, ketto;
		__pList->RemoveAllItems();
		enumerator->Reset();
		int i = 0;
		while (enumerator->MoveNext() == E_SUCCESS) {
			enumerator->GetStringAt(0, egy);
			enumerator->GetStringAt(1, ketto);

			arraylist.Add(egy);

			CustomListItem * newItem = new CustomListItem();

			newItem->Construct(100);
			newItem->SetItemFormat(*__pCustomListItemFormat);
			newItem->SetElement(TEXT_LISTENER, egy);
			newItem->SetElement(DELETE_LISTENER, *bitmapDeletePassive,
					bitmapDeleteActive);
			__pList->AddItem(*newItem, 0);

			__pList->SetItemEnabled(i++, true);
			this->RequestRedraw();
		}
	}
}

void MainForm::OnItemStateChanged(const Osp::Ui::Control& source, int index,
		int itemId, Osp::Ui::ItemStatus status) {
}

void MainForm::OnItemStateChanged(const Osp::Ui::Control& source, int index,
		int itemId, int elementId, Osp::Ui::ItemStatus status) {
	switch (elementId) {
	case DELETE_LISTENER: {
		AppLog("delete called, row: %d .", index);
		MessageBox msgbox;
		msgbox.Construct("Törlés", "Biztosan törlöd az elemet?", MSGBOX_STYLE_OKCANCEL, 0);
		int modalResult = 0;
		msgbox.ShowAndWait(modalResult);
		if(modalResult==MSGBOX_RESULT_OK){
			String a;
			arraylist.GetAt(index, a);
			databasehandler.Remove(a);
			RefreshList(sort);
		}
	}
	break;
	default:
	break;
	}
}
